#include <wgal.h>
#include <windows.h>

WGALVoid draw();
WGALVoid update(WGALUInt32);

WGALColor bgcolor = COLOR_WHITE;
WGALUInt32 image = ID_NONE;
WGALUInt32 font = ID_NONE;

WGALVoid wgalMain()
{
	WGALStartupInfoEx wsi;

	memset(&wsi, 0, sizeof(WGALStartupInfoEx));

	wsi.title = L"WGAL Test Application";
	wsi.width = 800;
	wsi.height = 600;
	wsi.fullscreen = FALSE;
	wsi.icon_id = ID_NONE;
	wsi.update_proc = &update;
	wsi.draw_proc = &draw;
	wsi.interval = FPS_TO_INTERVAL(30);

	if(!wgalSetupEx(&wsi))
	{
		// setup failed
		MessageBoxW(NULL,
			L"Could not initialize Direct3D device!",
			NULL,
			MB_OK | MB_ICONERROR);

		return;
	}

	// automatic present : default
	wgalSetAutoPresent(TRUE);

	// load image
	image = wgalLoadImage(L"C:\\test.jpg");

	// load font
	font = wgalLoadFont(L"Arial",20,FALSE,FALSE);

	// start message loop
	wgalStart();
}

WGALVoid draw()
{	
	wgalClear(bgcolor);
	wgalBlitEx(image,
		WGAL_DEFAULT,
		WGAL_DEFAULT,
		WGAL_DEFAULT,
		WGAL_DEFAULT,
		WGAL_FLIP_BOTH,
		COLOR_XRGB(128,128,255),
		0.0f,
		NULL);

	wgalDrawText(
		font,
		L"This is the string to be displayed.",
		NULL,
		WGAL_ALIGN_DEFAULT,
		COLOR_ARGB(255,128,128,128)
		);
}

WGALVoid update(WGALUInt32 frames)
{
	wgalUpdateInputState();

	// end the game on esc press
	if(wgalIsKeyPressed(WGAL_KC_ESCAPE))
		wgalExit();

	// toggle cursor visibility
	if(wgalIsKeyPressed(WGAL_KC_F1))
	{
		wgalShowCursor(!wgalIsCursorVisible());
	}

	// toggle fullscreen
	if(wgalIsKeyPressed(WGAL_KC_F2))
	{
		wgalToggleFullscreen();
	}

	// set background color
	if(wgalIsKeyPressed(WGAL_KC_R))
	{
		bgcolor = COLOR_RED;
	}
	else if(wgalIsKeyPressed(WGAL_KC_G))
	{
		bgcolor = COLOR_GREEN;
	}
	else if(wgalIsKeyPressed(WGAL_KC_B))
	{
		bgcolor = COLOR_BLUE;
	}
	else if(wgalIsKeyPressed(WGAL_KC_K))
	{
		bgcolor = COLOR_BLACK;
	}
	else if(wgalIsKeyPressed(WGAL_KC_W))
	{
		bgcolor = COLOR_WHITE;
	}
}